var vue = new Vue({
    el: '#app',
    data: {
        statusDic: {'0': '编辑', '1': '启用', '2': '停用'},
        leafDic: {'0': '否', '1': '是'},
        statusDicKey: {'EDIT': '0', 'VALID': '1', 'INVALID': '2'},
        dialogFormVisible: false,
        dialogFormVisibleAdd: false,
        formLabelWidth: '60px',
        labelPosition: 'right',
        useUrl: '2',
        loading: false, //区域加载
        selectNode:{},
        menuTreeData: [{
            name: '菜单管理',
            children: [{
                name: '系统管理',
                children: [{
                    name: '菜单管理',
                }, {
                    name: '角色管理',
                }, {
                    name: '人物管理',
                }]
            }]
        }],   //树形图

        treeProps: {
            children: 'menuChildren',
            label: 'name'
        },
        tableData: [{

        }],//表格数据
        menuForm: {
            id: '',
            parentId:'0',
            code: '',
            name: '',
            url: '',
            menuParams: '',
            moduleName: '',
            icon: '',
            busiStatus: '0',
            leaf: '1',
            openType: '',
            menuOrder: '',
            remark: ''
        },//表单数据

        paramData: {
            menuName: '',
            parentMenuCode: '',
            currentPage: 1, //当前页数
            pageSize: 5, //每页条数
            total: 0, //总条数
        },
        dialogTitle: '新增菜单',    //标志菜单功能
        //校验规则
        menuFormRules: {
            code: [
                {required: true, message: '请填写菜单编码'}
            ],
            name: [
                {required: true, message: '请填写菜单名称'},
            ],
            url: [
                {required: true, message: '请填写菜单路径'},
            ],
            icon: [
                {required: true, message: '请选择菜单图标'}
            ],
            leaf: [
                {required: true, message: '请选择菜单是否是叶子节点'},
            ],
            menuOrder: [
                {required: true, message: '请填写菜单序号'},
                {
                    message: '请填入正整数！',
                    pattern: /^[0-9]*[1-9][0-9]*$/,
                    trigger: 'blur'
                }],
            moduleName: [
                {required: true, message: '请填写应用模块名称'},
            ]
        },
        formRead: false,    //设置表单只读
        iconlist: [],   //图标集合
        openTypes: [],  //打开类型
        selectRoles: [],    //选择所属节点
        rolelist: [],       //角色列表

    },

    //钩子函数，VUE对象初始化完成后自动执行
    created() {
        this.initTree();//初始化树
        this.onSearch();//初始化菜单列表
        this.initRoles();//初始化角色列表

    },

    methods: {

        handleNodeClick(node) {
            //复制当前节点数据
            $.extend(this.selectNode, node);
           /* console.log("node")
            console.log(this.selectNode)*/
            //设置当前为第一页
            //console.log(node)
            this.paramData.current = 1;
            //判断是否为叶子节点
            if (node.leaf) {
                //叶子节点没有节点数据，集合置为空
                this.tableData = [];
                //分页总条数设为0
                this.paramData.totalRecord = 0;
            } else {
                //console.log("不是叶子节点")
                //表格查询
                this.loadTable(node);
            }

        },
        addMenu() {
            if (this.selectNode.code) {
                this.dialogTitle = '新增菜单';
                this.formRead = false;
                this.menuForm = {
                    id: '',
                    code: '',
                    parentMenuCode: this.selectNode.code,
                    menuName: '',
                    menuUrl: '',
                    menuParams: '',
                    icon: '',
                    moduleName: '',
                    busiStatus: '0',
                    leaf: this.selectNode.menuCode == 'VROOT' ? '0' : '1',
                    menuOrder: '',
                    remark: ''
                };
                //清空模型
                this.selectRoles = [];
                this.dialogFormVisibleAdd = true;
            } else {
                this.$message.error("请在左侧选择菜单的父节点!");
            }
        },

        allowDrop(draggingNode, dropNode) {

        },
        allowDrag: function (draggingNode) {

        },

        handleDrop: function (draggingNode, dropNode, dropType) {
            var self = this;
            var data = {
                fromMenuCode: draggingNode.data.menuCode,
                toMenuCode: dropNode.data.menuCode,
                dropType: dropType
            };
        },

        //初始化树
        initTree() {
            var me = this;
            //Util.postEncrypt('/system/menu/listAllMenuTree', null, function (result) {
            Util.postJson('/sysMenu/listMenuTree', null, function (result) {
                me.menuTreeData = result.data;
                console.log(result.data)
                if (result.success) {
                    me.menuTreeData = result.data;
                    console.log("999")
                    //console.log(self.selectNode.code)

                    /*self.menuTreeData = [result.obj];
                    if (self.selectNode.menuCode) {
                        self.$nextTick(function () {
                            self.$refs.menuTree.setCurrentNode(self.selectNode);
                        })
                    }*/
                }
            });
        },

        //初始角色
        initRoles() {
            var self = this;
            Util.postEncrypt('/system/role/listAllInternalRoles', null, function (result) {
                console.log(result.data)
                if (result.success) {
                    self.rolelist = result.data;
                }
            });
        },
        //表格数据获取
        loadTable(node) {

            console.log(node)
            var me = this;
            console.log("000")
            //console.log(code)
            //console.log(me.selectNode.code)
            //console.log(node.code || me.selectNode.code)
            //me.paramData.parentMenuCode = (code || me.selectNode.code);
            //判断节点是否相同
            /*if (!me.paramData.parentMenuCode) {
                return;
            }*/
            Util.post('/sysMenu/listNextLevelMenuByPage?'+ "currentPage="
                + this.paramData.currentPage + "&pageSize=" + this.paramData.pageSize,{"id":node.id}, function (result) {
                console.log(result.data);
                if (result.success) {
                    me.tableData = result.obj.records;
                    //设置分页总条数
                    me.paramData.total = result.obj.total;
                }
            });

        },
        //当前页码
        handleSizeChange(pageSize) {
            console.log(pageSize);
            this.paramData.pageSize = pageSize;
            //查询表格
            this.onSearch();
        },
        //分页总页数改变
        handleCurrentChange(CurrentPage) {
            console.log(CurrentPage);
            this.paramData.currentPage = CurrentPage;
            //查询表格
            this.onSearch();
        },
        //树被点击时的回调
        handleDialogClose() {

        },

        //拖拽成功完成时触发的事件
        handleDrop: function (draggingNode, dropNode, dropType) {
            var self = this;
            var data = {
                fromMenuCode: draggingNode.data.menuCode,
                toMenuCode: dropNode.data.menuCode,
                dropType: dropType
            };
            /*Util.post('/system/menu/updateParent', data, function (result) {
                if (result.success) {
                    self.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                    self.loadTable();
                } else {
                    self.$message.error(result.msg);
                }
            });*/
        },
        allowDrop(draggingNode, dropNode) {
            return !dropNode.data.leaf;
        },
        allowDrag(draggingNode) {
            return draggingNode.data.leaf;
        },
        //多条件查询
        onSearch() {
            var menus = {
                name : this.paramData.menuName,
                code : this.paramData.menuCode
            };
            var aa = this;
            Util.postJson('/sysMenu/listLikeMenuByPage?'+ "currentPage="
                + this.paramData.currentPage + "&pageSize=" + this.paramData.pageSize,menus,function (result) {
                aa.tableData = result.obj.records;
                aa.paramData.total = result.obj.total;
                console.log(result.data)
            });
        },

        //查看
        openDetail(row) {
            this.dialogTitle = '查看菜单';
            this.dialogFormVisible = true;
            this.menuForm = row;
            this.formRead = true;   //设置只读
            //console.log(row)
        },
        //编辑
        editDetail(row) {
            this.formRead = false,
            this.dialogTitle = '编辑菜单';
            this.dialogFormVisible = true;
            this.menuForm = row;

        },
        //删除
        deleteRow(m) {
            var self = this;

            var node={
                id:"undefined"?1:self.selectNode.id,
            }
            console.log(node.id)
            var id = m.id;
            self.$confirm('确定删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(function () {
                Util.get('/sysMenu/deleteMenu/'+id,{}, function (result) {
                    console.log(result)
                    if (result.success) {
                        self.$message({
                            type: 'success',
                            message: '菜单删除成功!'
                        });
                        self.loadTable(node);
                        self.initTree();
                    } else {
                        self.$message.error(result.msg);
                    }
                });
            }).catch(function () {
                self.$message({
                    type: 'info',
                    message: '取消删除!'
                });
            });
        },

        leafRender: function (value) {
            return this.leafDic[value];
        },

        //保存表单
        save() {
            var self = this;
            self.$refs['menuForm'].validate(function (valid) {
                if (valid) {
                    console.log(self.menuForm)
                    /*self.menuForm.remark = self.selectRoles.join(',');
                    self.menuForm.belongArea = self.selectBelongs.join(',');*/
                    Util.postJson('/sysMenu/updateMenu', self.menuForm, function (result) {
                        console.log(result)
                        if (result.success) {
                            self.$message({
                                type: 'success',
                                message: '添加成功!'
                            });
                            self.dialogFormVisible = false;
                            //修改成功后刷新树结构和表格
                            self.initTree();
                            self.loadTable();
                        } else {
                            self.$message.error(result.msg)
                        }
                    });
                } else {
                    return false;
                }
            });
        },
        newSave(){
            var self = this;

            self.menuForm.parentId = self.selectNode.id;
            self.$refs['menuForm'].validate(function (valid) {
                if (valid) {
                    /*self.menuForm.remark = self.selectRoles.join(',');
                    self.menuForm.belongArea = self.selectBelongs.join(',');*/
                    console.log(self.menuForm)
                    Util.postJson('/sysMenu/addMenu', self.menuForm, function (result) {
                        if (result.success) {
                            self.$message({
                                type: 'success',
                                message: '操作成功!'
                            });
                            console.log(self.dialogFormVisible)
                            self.dialogFormVisibleAdd = false;
                            //修改成功后刷新树结构和表格
                            self.initTree();
                            self.loadTable(self.selectNode);
                        } else {
                            self.$message.error(result.msg)
                        }
                    });
                } else {
                    return false;
                }
            });
        }


    }
})